﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ReviewCurveApp.Classes;
using ReviewCurveApp.Contorller;

namespace ReviewCurveApp
{
    public partial class FrmSubjectView : Form
    {
        public FrmSubjectView()
        {
            InitializeComponent();
        }

        Subject _Subject;

        UnitsManager unitsManager = new UnitsManager();
        SubjectsManager subjectsManager = new SubjectsManager();
        internal void SetSubject(Subject subject)
        {
            _Subject = subject;

        }

        private void ClearUI()
        {
            textBox_Desc.Text = "";
            textBox_Destination.Text = "";
            textBox_Name.Text = "";
        }

        private void FrmSubjectView_Load(object sender, EventArgs e)
        {
            PaintSubject();
            SetEditable(false);
        }

        private void PaintSubject()
        {
            ClearUI();
            if (_Subject==null)
            {
                return;

            }

            textBox_Desc.Text = _Subject.Description;
            textBox_Destination.Text = _Subject.Destination;
            textBox_Name.Text = _Subject.Name;
            LoadUnits(_Subject);
        }

        private void LoadUnits(Subject subject)
        {
            ClearUnits();
            List<Unit> units = unitsManager.GetUnits(subject);
            for (int i = 0; i < units.Count; i++)
            {
                Unit unit = units[i];
                
                ListViewItem item = new ListViewItem();
                item.Tag = unit;
                item.Text = (i+1).ToString();
                item.SubItems.Add( unit.Name+" "+unit.Description);

                listView1.Items.Add(item);
            }
            listView1.AutoResizeColumns(ColumnHeaderAutoResizeStyle.ColumnContent);
        }

        private void ClearUnits()
        {
            listView1.Items.Clear();
        }

        private void editToolStripMenuItem_Click(object sender, EventArgs e)
        {
            SetEditable(true);
            
        }



        private void SetEditable(bool editable)
        {
            panelMainInfo.Enabled = editable;
            button_Commit.Visible = editable;
            button_Cancel.Visible = editable;
        }

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.OK;
        }

        private void toolStripButton_Create_Click(object sender, EventArgs e)
        {
            FrmAddUnit frmAddUnit = new FrmAddUnit();
            frmAddUnit.SetSubject(_Subject);
            if (frmAddUnit.ShowDialog() == DialogResult.OK)
            {
                LoadUnits(_Subject);
            }
        }

        private void button_Commit_Click(object sender, EventArgs e)
        {
            Subject subject = new Subject();

            subject.Id = _Subject.Id;

            subject.Name = textBox_Name.Text;

            subject.Description = textBox_Desc.Text;

            subject.Destination = textBox_Destination.Text;



            if (!subjectsManager.UpdateSubject(subject))
            {
                MessageBox.Show("edit failed !");
                return;
            }

            MessageBox.Show("edit Successed !");

            SetEditable(false);
        }

        private void button_Cancel_Click(object sender, EventArgs e)
        {
            PaintSubject();
            SetEditable(false);
        }

        private void listView1_DoubleClick(object sender, EventArgs e)
        {
            Unit unit = GetSelectedUnit();
            FrmUnitView frmUnitView = new FrmUnitView();
            frmUnitView.SetUnit(unit);
            if (frmUnitView.ShowDialog() == DialogResult.OK)
            {
                LoadUnits(_Subject);
            }
        }

        private Unit GetSelectedUnit()
        {
            return listView1.SelectedItems[0].Tag as Unit;
        }



    }
}
